package com.example.elm.Account

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.elm.Public.StoreInfo
import com.example.elm.Public.AccountInfo
import com.example.elm.R
import com.example.elm.databinding.StoreFollowBinding

private val storeList = ArrayList<StoreInfo>()
class FollowstoreActivity : ComponentActivity() {
    private lateinit var binding: StoreFollowBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = StoreFollowBinding.inflate(layoutInflater)
        setContentView(binding.root)

        init_store()
        val AccountInformation = CommonUtil.getSerializable(this, "info", AccountInfo::class.java)
        val layoutManager = LinearLayoutManager(this)
        binding.followstoreUi.layoutManager = layoutManager
        val adapter = StoreAdapter(storeList)
        binding.followstoreUi.adapter = adapter

        binding.back.setOnClickListener {
            val intent = Intent()
            intent.putExtra("info", AccountInformation)
            setResult(RESULT_OK, intent)
            finish()
        }


    }

    private fun init_store() {
        storeList.clear()
        storeList.add(StoreInfo("cqxm","重庆小面","45分钟","1.2km",4.8,950,10,"免配送费","包装好评率95%",1))
        storeList.add(StoreInfo("mtf","干饭人拌饭-木桶饭(农大店)","26分钟","825m",4.7,3000,20,"免配送费","近30日235个回头客",0))
        storeList.add(StoreInfo("hnnrt","淮南牛肉汤","37分钟","942m",5.0,3000,0,"免配送费","口味正宗",0))
    }

    private class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val image: ImageView = itemView.findViewById(R.id.imageView)
        val name: TextView = itemView.findViewById(R.id.tvName)
        val delivertTime: TextView = itemView.findViewById(R.id.tvDeliveryTime)
        val distance: TextView = itemView.findViewById(R.id.tvDistance)
        val rating: TextView = itemView.findViewById(R.id.tvRating)
        val sales: TextView = itemView.findViewById(R.id.tvSales)
        val minPrice: TextView = itemView.findViewById(R.id.tvMinPrice)
        val deliveryCost: TextView = itemView.findViewById(R.id.tvDeliveryCost)
        val recommend: TextView = itemView.findViewById(R.id.tvRecommend)
        val campusSend: TextView = itemView.findViewById(R.id.tvCampusSend)
    }
    private class StoreAdapter(val storeList: ArrayList<StoreInfo>): RecyclerView.Adapter<ViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.store_follow_ui, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val storeInfo = storeList[position]
            val imageResId = holder.itemView.context.resources.getIdentifier(
                storeInfo.image, "drawable", holder.itemView.context.packageName
            )
            if (imageResId != 0) {
                holder.image.setImageResource(imageResId)
            } else {
                holder.image.setImageResource(R.drawable.cqxm)
            }
            holder.name.text = storeInfo.name
            holder.delivertTime.text = storeInfo.deliveryTime
            holder.distance.text = storeInfo.distance
            holder.rating.text = "${storeInfo.rating}分"
            holder.sales.text = "月售${storeInfo.sales}+"
            holder.minPrice.text = "起送￥${storeInfo.minPrice}"
            holder.deliveryCost.text = storeInfo.deliveryCost
            holder.recommend.text = storeInfo.recommend
            holder.campusSend.text = if (storeInfo.campusSend > 0) "校园送" else ""
        }

        override fun getItemCount(): Int {
            return storeList.size
        }
    }
}